package com.lagou.edu.filter;

import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * ip防刷爆filter
 * @author caiwei
 * @date 2020-11-18 22:10
 */
@Component
@RefreshScope
public class IpRequestGlobalFilter implements GlobalFilter, Ordered {

    @Value("${config.request.iplimit.apis}")
    private String apiPath;

    @Value("${config.request.iplimit.intervalMinutes}")
    private int intervalMinutes;

    @Value("${config.request.iplimit.maxRequestCount}")
    private int maxRequestCount;

    /**
     * String为key,存储对应的IP
     * List<Long>为value,存储多次请求的时间
     */
    private static ConcurrentHashMap<String, List<Long>> ipRegistryMap = new ConcurrentHashMap<>();

    private static List<Long> ipRegistryList = new ArrayList<>();

    @SneakyThrows
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        System.out.println("========进入ip防刷爆过滤器==========");

        //从上下文中取出request对象和response对象
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();

        //从request对象中获取客户端IP
        String clientIp = request.getRemoteAddress().getHostString();
        String path = request.getURI().getPath();
        if(path.startsWith(apiPath)){
            //当前时间往前推一分钟
            long currentTime = System.currentTimeMillis();
            long beforeOneMinuteTime = currentTime-intervalMinutes*60*1000;
            List<Long> list = ipRegistryMap.get(clientIp);
            Integer count = 0;
            if(list != null && list.size()>0){
                for (Long time : list) {
                    if(time >= beforeOneMinuteTime && time <= currentTime){
                        count++;
                    }
                }
            }
            if(count>maxRequestCount){
                //状态码-303
                System.out.println("============服务器拒绝访问=============");
                response.setStatusCode(HttpStatus.SEE_OTHER);
                String data = "您频繁进行注册,请求已被拒绝";
                DataBuffer wrap = response.bufferFactory().wrap(data.getBytes("UTF-8"));
                return response.writeWith(Mono.just(wrap));
            }
            ipRegistryList.add(currentTime);
            System.out.println("=============添加IP请求时间戳，ipRegistryList="+ipRegistryList.size()+"======");
            ipRegistryMap.put(clientIp,ipRegistryList);

        }
        //合法请求放行，继续执行后续的过滤器
        return chain.filter(exchange);
    }


    /**
     * 返回值表示当前过滤器的顺序（优先级），数值越小，优先级越高
     * @return  int
     */
    @Override
    public int getOrder() {
        return 0;
    }
}
